<?php

namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\SetPasswordRequest;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class DashboardController extends Controller
{
    
	public function index(Request $request)
    {
        $merchant = auth('admin')->user(); // 商户ID
        $merchantId = $merchant->id;
        $where = [];
        //管理员登录显示所有数据
        $where[] = ['merchant_orders.operation_id','=', 1];
        if($merchant->type == 4 ){
            $where[] = ['merchant_orders.operation_id','=',$merchantId];
        }
        $whereInStr = '';
        $whereInArr = [];
        if($merchant->type == 3 ){
            $ywusers =  DB::table('admin_users')->select('id')->where('parent_id',$merchantId)->get();
            $ywids = [];
            if($ywusers){
                foreach ($ywusers as $k => $v){
                    $ywids[] = $v->id;
                }
            }
            array_push($ywids,$merchantId);
            $whereInStr = 'merchant_orders.merchant_id';
            $whereInArr = $ywids;
        }
        if($merchant->type == 2){
           $users =  DB::table('admin_users')->select('id')->where('parent_id',$merchantId)->get();
           $shids = [];
           if($users){
                foreach ($users as $k => $v){
                   $shids[] = $v->id;
                }
           }
           array_push($shids,$merchantId);
           $whereInStr = 'merchant_orders.merchant_id';
           $whereInArr = $shids;   
        }    
        //今日统计
        $startToday = date('Y-m-d', time()) . ' 00:00:00';
		$endToday = date('Y-m-d', time()) . ' 23:59:59';
        //昨日统计
		$yesterday = date("Y-m-d",strtotime("-1 day")) . ' 00:00:00';
        $yesToday = date("Y-m-d",strtotime("-1 day")) . ' 23:59:59';
        //本月收入
        $startMoon = date('Y-m-01', strtotime(date("Y-m-d"))) . ' 00:00:00';
        $endMoon = date('Y-m-d', strtotime("$startMoon +1 month -1 day")) . ' 23:59:59';
        //上月收入
        $yesterdayMoon = date('Y-m-01', strtotime(date("Y-m-d",strtotime("-1 day")))) . ' 00:00:00';
        $yesMoon = date('Y-m-d', strtotime("$startMoon +1 month -1 day")) . ' 23:59:59';
        
        if(1 == $merchant->type){
            //今日收入
            $todayOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startToday)
                ->where('orders.created_at', '<=', $endToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->SUM('orders.goods_total_price');
            //今日订单D
            $todayOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startToday)
                ->where('orders.created_at', '<=', $endToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->count();
            //昨天累计金额
            $yesterdayOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterday)
                ->where('orders.created_at', '<=', $yesToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->SUM('orders.goods_total_price');
            // 昨天订单D
            $yesterdayOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterday)
                ->where('orders.created_at', '<=', $yesToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->count();
            $moonOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startMoon)
                ->where('orders.created_at', '<=', $endMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->SUM('orders.goods_total_price');
            //本月销量
            $moonOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startMoon)
                ->where('orders.created_at', '<=', $endMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->count();
            //上月销量
            $yesmoonOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterdayMoon)
                ->where('orders.created_at', '<=', $yesMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->count();
            //在线设备
            //$machines = DB::table('machine_managers')->select('machine_id')->where($where)->where('manager_id', $merchantId)->get();
            
            if($merchantId != 1){
                $machines = DB::table('machine_managers')->select('machine_id')->where('manager_id', $merchantId)->get();
            }else{
                $machines = DB::table('machine_managers')->select('machine_id')->get();
            }
            //dd($machines);die;
            $onMachine = 0;
            $downMachine = 0;
            if ($machines){
                $machineIds = [];
                foreach ($machines as $v){
                    $machineIds[] = $v->machine_id;
                }
                if(!empty($machineIds)){
                    $onMachine = DB::table('machines')->whereIn('id', $machineIds)->where('connected_at', '!=', '')->count();
                    $downMachine = DB::table('machines')->whereIn('id', $machineIds)->count();
                }
            }  

        }else if($merchant->type != 4 && 1 != $merchant->type){
             //今日收入
             $todayOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startToday)
                ->where('orders.created_at', '<=', $endToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->SUM('orders.goods_total_price');
            //今日订单D
            $todayOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startToday)
                ->where('orders.created_at', '<=', $endToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->count();
            //昨天累计金额
            $yesterdayOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterday)
                ->where('orders.created_at', '<=', $yesToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->SUM('orders.goods_total_price');
            //昨天订单D
            $yesterdayOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterday)
                ->where('orders.created_at', '<=', $yesToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->count();
            $moonOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startMoon)
                ->where('orders.created_at', '<=', $endMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->SUM('orders.goods_total_price');
            //本月销量
            $moonOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startMoon)
                ->where('orders.created_at', '<=', $endMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->count();
            //上月销量
            $yesmoonOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterdayMoon)
                ->where('orders.created_at', '<=', $yesMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->whereIn($whereInStr,$whereInArr)
                ->count();   
            //在线设备
            $machines = DB::table('machine_managers')->select('machine_id')->where('manager_id', $merchantId)->get();
            //dd($machines);die;
            $onMachine = 0;
            $downMachine = 0;
            if ($machines){
                $machineIds = [];
                foreach ($machines as $v){
                    $machineIds[] = $v->machine_id;
                }
                if(!empty($machineIds)){
                    $onMachine = DB::table('machines')->whereIn('id', $machineIds)->where('connected_at', '!=', '')->count();
                    $downMachine = DB::table('machines')->whereIn('id', $machineIds)->count();
                }
            }  

        }else{
            // 今日收入
            $todayOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startToday)
                ->where('orders.created_at', '<=', $endToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->SUM('orders.goods_total_price');
            // 今日订单D
            $todayOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startToday)
                ->where('orders.created_at', '<=', $endToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->count();
            //昨天累计金额
            $yesterdayOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterday)
                ->where('orders.created_at', '<=', $yesToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->SUM('orders.goods_total_price');
            // 昨天订单D
            $yesterdayOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterday)
                ->where('orders.created_at', '<=', $yesToday)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->count();
            $moonOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startMoon)
                ->where('orders.created_at', '<=', $endMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->SUM('orders.goods_total_price');
            // 本月销量
            $moonOrders = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $startMoon)
                ->where('orders.created_at', '<=', $endMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->count();
            // 上月销量
            $yesmoonOrderTotalPrice = DB::table('merchant_orders')
                ->leftjoin('orders', 'orders.id', '=', 'merchant_orders.order_id')
                ->where('orders.created_at', '>=', $yesterdayMoon)
                ->where('orders.created_at', '<=', $yesMoon)
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[2])
                ->where($where)
                ->count();
                
            //在线设备
            $machines = DB::table('machine_managers')->select('machine_id')->where('manager_id', $merchantId)->get();
            //dd($machines);die;
            $onMachine = 0;
            $downMachine = 0;
            if ($machines){
                $machineIds = [];
                foreach ($machines as $v){
                    $machineIds[] = $v->machine_id;
                }
                if(!empty($machineIds)){
                    $onMachine = DB::table('machines')->whereIn('id', $machineIds)->where('connected_at', '!=', '')->count();
                    $downMachine = DB::table('machines')->whereIn('id', $machineIds)->count();
                }
            }  
        }
        
        return view('admins.index.index')
			->with('todayOrderTotalPrice', $todayOrderTotalPrice)//本月收入
			->with('yesterdayOrderTotalPrice', $yesterdayOrderTotalPrice)//上月收入
			->with('todayOrders', $todayOrders)//本月订单
			->with('yesterdayOrders', $yesterdayOrders)//上月订单
            ->with('moonOrderTotalPrice', $moonOrderTotalPrice)
			->with('moonOrders', $moonOrders)
			->with('yesmoonOrderTotalPrice', $yesmoonOrderTotalPrice)//上月总销量
            ->with('onMachine', $onMachine)
            ->with('downMachine', $downMachine);
    }
    
	public function setPassword()
	{
		return view('admins.index.setPassword');
	}

	public function setPasswordStore(SetPasswordRequest $request)
	{
		$manager = auth('admin')->user();
		$manager->password = encrypt_str($request->password);
		$manager->save();
		auth('admin')->logout();
		return redirect()->route('admin.relogin')->with('success', '添加成功');
	}

}
